//=============================================================================

#include <string.h>
#include "../../../../inc/aPacket.h"

//=============================================================================

aPacket::aPacket() :  longSize(sizeof(long)), floatSize(sizeof(float)), boolSize(sizeof(bool)),
                      charSize(sizeof(char))
{
  length = 0;
  data = 0;
  type = PAK_NULL;
}

//=============================================================================

aPacket::aPacket(aptType t) : longSize(sizeof(long)), floatSize(sizeof(float)), boolSize(sizeof(bool)),
                              charSize(sizeof(char))
{
  length = 0;
  data = 0;
  type = t;
}

//=============================================================================

aPacket::aPacket(char* in) :  longSize(sizeof(long)), floatSize(sizeof(float)), boolSize(sizeof(bool)),
                              charSize(sizeof(char))
{
  apChewPacket(in);
}

//=============================================================================

aPacket::aPacket(const aPacket& p) :  longSize(sizeof(long)), floatSize(sizeof(float)), boolSize(sizeof(bool)),
                                      charSize(sizeof(char))
{
  apChewPacket(p.apGetData());
}

//=============================================================================

void aPacket::apClearPacket()
{
  if (data)
  {
    delete [] data;
    data = 0;
  }

  length = 0;
  type = PAK_NULL;
}

//=============================================================================

void aPacket::apChewPacket(char* in)
{
  if (in)
  {
    memcpy(&length, &in[1], longSize);
    type = aptType(in[0]);
    data = new char[length + 1];
    memcpy(data, in, length);
    data[length] = 0;
  }
  else
  {
    length = 0;
    data = 0;
    type = PAK_NULL;
  }
}

//=============================================================================

const aPacket& aPacket::operator=(const aPacket& p)
{
  apClearPacket();

  length = p.apGetLength();
  data = new char[length];
  memcpy(data, p.apGetData(), length);
  type = p.apGetType();

  return (*this);
}

//=============================================================================

aPacket::~aPacket()
{
  apClearPacket();
}

//=============================================================================
